package com.atguigu.lease.web.admin.custom.config;

import com.atguigu.lease.web.admin.custom.conterver.StringToBaseEnumConverterFactory;
import com.atguigu.lease.web.admin.custom.conterver.StringToItemTypeConverter;
import com.atguigu.lease.web.admin.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Autowired
    private StringToItemTypeConverter stringToItemTypeConverter;//注入了一个自定义的类型转换器
    @Autowired
    private StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;
    @Value("${login.interceptor.includes}")
    private String[] includes;
    @Value("${login.interceptor.excludes}")
    private String[] excludes;
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(stringToBaseEnumConverterFactory);//添加自定义的类型转换器
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())//创建拦截器对象
                .addPathPatterns(includes)//添加拦截路径
                .excludePathPatterns(excludes)//添加不拦截路径
                .order(10);//拦截器执行顺序优先级：数字越小优先级越高
    }
}
